package com.wy.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class MqFongconfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //从应用上下文中获取一个类型为RabbitTemplate的bean
        RabbitTemplate bean = applicationContext.getBean(RabbitTemplate.class);
        //配置回调
        bean.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int replyCode, String replyText, String
                    exchange, String routingKey) {
                log.debug("收到消息的 return callback - 交换器：{}，路由键：{}，回复代码：{}，回复文本：{}",
                        exchange, routingKey, replyCode, replyText);
                if (message!= null) {
                    log.debug("消息内容：{}", new String(message.getBody()));
                }
            }
        });
    }
}
